package com.wuliang.generate.common.core.throwable.handler;

import com.wuliang.generate.common.core.basic.BasicExceptionHandler;
import com.wuliang.generate.common.core.response.Result;
import com.wuliang.generate.common.core.response.ResultUtils;
import com.wuliang.generate.common.core.response.ReturnCode;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.JwtException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author WuLiang
 */
@Slf4j
@RestControllerAdvice
public class JwtExceptionHandler implements BasicExceptionHandler<JwtException> {

    @Override
    public Result<?> exceptionHandler(JwtException e) {
        if (e instanceof ExpiredJwtException) {
            return tokenExceptionHandler(e);
        }
        return ResultUtils.systemError("");
    }

    @ExceptionHandler({ ExpiredJwtException.class })
    public Result<?> tokenExceptionHandler(Throwable e) {
        return new Result<>("已过期", ReturnCode.SYSTEM_EXCEPTION);
    }
}
